# tests/conftest.py

import pytest
from unittest.mock import MagicMock

# 未来可以在这里添加共享的 fixtures，例如模拟的 FEniCSx 对象
# 或通用的 SimulationParameters 实例。

# 示例 fixture (如果需要):
# @pytest.fixture(scope="session")
# def mock_comm():
#     """提供一个模拟的 MPI communicator。"""
#     class MockComm:
#         def Get_rank(self):
#             return 0
#         def Get_size(self):
#             return 1
#     return MockComm()

# @pytest.fixture
# def basic_simulation_parameters():
#     """提供一个基本的 SimulationParameters 实例。"""
#     from src.core.simulation_parameters import SimulationParameters
#     # 可以从一个默认的配置文件加载或直接在这里定义
#     return SimulationParameters() # 假设它可以无参数实例化或有默认值

# --- 模拟 FEniCSx 和 MPI 相关的类 ---
class MockFEMFunction:
    def __init__(self, name="mock_function"):
        self.name = name
        self.x = MagicMock()
        self.x.array = MagicMock()
        self.x.array.size = 10
        self.x.array.min = MagicMock(return_value=0.0)
        self.x.array.max = MagicMock(return_value=1.0)
        self.x.array.mean = MagicMock(return_value=0.5)
        self.x.array.__pow__ = MagicMock(return_value=self.x.array)
        self.x.array.sum = MagicMock(return_value=5.0)
        self.x.array.std = MagicMock(return_value=0.2)

class MockMesh:
    pass

class MockMPIComm:
    rank = 0
    size = 1
    def __init__(self): # 添加构造函数以初始化 MagicMock
        self.bcast = MagicMock(side_effect=lambda data, root=0: data)
        self.Barrier = MagicMock()
        self.allreduce = MagicMock(side_effect=lambda data, op=None: data)

# --- Fixtures ---
@pytest.fixture
def mock_sim_params_base():
    """提供一个基础的模拟 SimulationParameters 实例。"""
    mock_params = MagicMock()
    mock_params.output_frequency = 1
    mock_params.some_other_param = "test_value"
    mock_params.stats_calc_frequency = 1
    mock_params.stats_output_filename = "default_stats.json"
    mock_params.functions_to_analyze = ["phi_s", "temperature"]
    mock_params.calculate_global_norm = True
    mock_params.vtk_output_frequency = 1
    mock_params.vtk_filename_prefix = "default_vtk_sol"
    return mock_params

@pytest.fixture
def mock_fem_entities_fenicsx():
    """提供包含模拟 FEniCSx fem.Function 的 FEM 实体。"""
    return {
        "phi_s": MockFEMFunction(name="phi_s_test"),
        "temperature": MockFEMFunction(name="temp_test"),
        "mesh": MockMesh()
    }

@pytest.fixture
def mock_fem_entities_no_fenicsx():
    """提供不含 FEniCSx 对象的 FEM 实体。"""
    return {
        "phi_s": "这是一个字符串模拟对象",
        "mesh": "这也是一个字符串"
    }

@pytest.fixture
def mock_comm():
    """提供一个模拟的 MPI 通信器。"""
    return MockMPIComm()

print("tests/conftest.py 加载完毕") 